/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-12-01 16:57:14
 * @filename : service/ztree.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    var ztree = require('ztree'),
        ztreeMC = require('{model}ztree');

    require('ztree/3.5.0/zTreeStyle/zTreeStyle.css');
    
    var ztreeC = new ztreeMC.ztreeC;
    var curMenu = null, zTree_Menu = null;

    var ZtreeService = {

        init : function(obj){
             ztreeC.add(this.ztreeData);
             var curMenu,treeObj = $("#treeMenu"),
                 zNodes = ztreeC.toJSON();
             $.fn.zTree.init(treeObj, this.setting, zNodes);
             zTree_Menu = $.fn.zTree.getZTreeObj("treeMenu");
             curMenu = this.getSelectNode(obj,zTree_Menu); 
             zTree_Menu.selectNode(curMenu);          
             treeObj.addClass("showIcon");
        },

        getSelectNode : function(obj,zTree_Menu) {
            var name,node; 
            if (obj.name == 'index' && obj.val == undefined) {
                name = 'gold_monitor';
            } else if (obj.val !== undefined) {
                name = obj.val; 
            }

            var obj = zTree_Menu.getNodes();

            for (var i in obj ) {
                 var fobj = obj[i].children;
                 for (var j in fobj) {
                      if (fobj[j].module == name) {
                          node = fobj[j];  
                          break;
                      }
                 }
            }
            return node;
        },

        setting : {
             view: {
                showLine: false,
                showIcon: false,
                selectedMulti: false,
                dblClickExpand: false,
                addDiyDom: function(treeId,treeNode) {
                    //console.log('addDiyDom',treeId,treeNode);
                    var spaceWidth = 5;
                    var switchObj = $("#" + treeNode.tId + "_switch"),
                    icoObj = $("#" + treeNode.tId + "_ico");
                    switchObj.remove();
                    icoObj.before(switchObj);
                    if (treeNode.level > 0) {
                        var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
                        switchObj.before(spaceStr);
                    }
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: function(evt,treeId, treeNode) {
                    var module = treeNode.module;
                    //console.log('click',treeNode); 
                    if (treeNode.children) {
                        var zTree_Menu = $.fn.zTree.getZTreeObj("treeMenu");  
                        zTree_Menu.expandNode(treeNode);
                        return false;
                    } else {
                        if (module) {
                            Backbone.routerObj.navigate(module+'/i', { trigger: true });
                            return;
                        }
                    }
                }
            }
        },
        
        ztreeData : [
            { id:1, pId:0, name:"Real Time Monitor"},
            { id:11, pId:1, name:"Gold Monitor", module:"gold_monitor"},
            { id:2, pId:0, name:"Gold Trend"},
            { id:16, pId:2, name:"Total Gold",module:"total_gold"},
            { id:17, pId:2, name:"Subtotal Gold",module:"subtotal_gold"},
            { id:18, pId:2, name:"Gold Ratio",module:"gold_ratio"},
            { id:19, pId:2, name:"PS & MM vs All",module:"other_vs_all"},
            { id:20, pId:2, name:"Auto Renew",module:"auto_renew"},
            { id:21, pId:2, name:"Manual Renew",module:"manual_renew"},
            { id:3, pId:0, name:"Profile Analysis"},
            { id:22, pId:3, name:"Total Profile",module:"total_profile"},
            { id:12, pId:3, name:"Subtotal Profile",module:"subtotal_profile"},
            { id:4, pId:0, name:"Email Monitor"},
            { id:13, pId:4, name:"Aol Spam Overview",module:"aol_spam_overview"},
            { id:14, pId:4, name:"Aol Spam By Site",module:"aol_spam_by_site"},
            { id:15, pId:4, name:"Aol Spam By Template",module:'aol_spam_by_title'},
        ],

    };

	module.exports = ZtreeService;
})

